package com.hzp.micromall.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzp.micromall.common.core.api.CommonPage;
import com.hzp.micromall.product.dao.PmsBrandDao;
import com.hzp.micromall.product.dao.PmsProductDao;
import com.hzp.micromall.product.dto.PmsBrandParam;
import com.hzp.micromall.product.entity.PmsBrand;
import com.hzp.micromall.product.entity.PmsProduct;
import com.hzp.micromall.product.service.PmsBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author houzipeng
 * @Date 2022/2/26 14:34
 * @Version 1.0
 * 商品品牌Service实现类
 */
@Service
public class PmsBrandServiceImpl implements PmsBrandService {

    @Autowired
    private PmsBrandDao brandDao;
    @Autowired
    private PmsProductDao productDao;

    @Override
    public int createBrand(PmsBrandParam pmsBrandParam) {
        PmsBrand pmsBrand = new PmsBrand();
        BeanUtil.copyProperties(pmsBrandParam, pmsBrand);
        //如果创建时首字母为空，取名称的第一个为首字母
        if (StringUtils.isEmpty(pmsBrand.getFirstLetter())) {
            pmsBrand.setFirstLetter(pmsBrand.getName().substring(0, 1));
        }
        return brandDao.insert(pmsBrand);
    }

    @Override
    public int deleteBrand(Long id) {
        return brandDao.deleteById(id);
    }

    @Override
    public int deleteBrand(List<Long> ids) {
        LambdaQueryWrapper<PmsBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(PmsBrand::getId, ids);
        return brandDao.delete(wrapper);
    }

    @Override
    public int updateBrand(Long id, PmsBrandParam pmsBrandParam) {
        PmsBrand pmsBrand = new PmsBrand();
        BeanUtil.copyProperties(pmsBrandParam, pmsBrand);
        pmsBrand.setId(id);
        //如果创建时首字母为空，取名称的第一个为首字母
        if (StrUtil.isEmpty(pmsBrand.getFirstLetter())) {
            pmsBrand.setFirstLetter(pmsBrand.getName().substring(0, 1));
        }
        //更新品牌时要更新商品中的品牌名称
        PmsProduct product = new PmsProduct();
        product.setBrandName(pmsBrand.getName());
        LambdaQueryWrapper<PmsProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProduct::getBrandId, id);
        productDao.update(product, wrapper);
        return brandDao.updateById(pmsBrand);
    }

    @Override
    public int updateShowStatus(List<Long> ids, Integer showStatus) {
        PmsBrand pmsBrand = new PmsBrand();
        pmsBrand.setStatus(showStatus);
        LambdaQueryWrapper<PmsBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(PmsBrand::getId, ids);
        return brandDao.update(pmsBrand, wrapper);
    }

    @Override
    public CommonPage<PmsBrand> getPage(String keyword, int pageNum, int pageSize) {
        Page<PmsBrand> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<PmsBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(PmsBrand::getSort);
        if (StrUtil.isNotEmpty(keyword)){
            wrapper.like(PmsBrand::getName, keyword);
        }
        brandDao.selectPage(page, wrapper);
        return new CommonPage<>(page.getCurrent(), page.getTotal(), page.getSize(), page.getRecords());
    }

    @Override
    public PmsBrand getBrand(Long id) {
        return brandDao.selectById(id);
    }

    @Override
    public List<PmsBrand> listAllBrand() {
        return brandDao.selectList(null);
    }
}
